package com.universal_library.activity;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.Window;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestManager;
import com.universal_library.utils.ImageLoader;
import com.universal_library.utils.statusBarM.StatusBarUtil;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by solin on 2018/1/29.
 */

public abstract class BaseActivity extends AppCompatActivity {
    private RequestManager mImageLoader;
    public static List<Activity> activities = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        initBundle(savedInstanceState);
        setContentView(getLayoutId());
//        ButterKnife.bind(this);
        initWindow();
        initView();
        initData();
        activities.add(this);
    }

    /**
     * 接收数据处理
     */
    protected void initBundle(Bundle bundle) {

    }

    /**
     * 初始化窗口
     */
    protected void initWindow() {
        StatusBarUtil.StatusBarLightMode(this, true);
    }

    //获取布局文件
    protected abstract int getLayoutId();

    //初始化 控件
    protected abstract void initView();

    //获取数据
    protected abstract void initData();

    /**
     * 加载网络图片
     *
     * @return
     */
    protected synchronized RequestManager getmImageLoader() {
        if (mImageLoader == null)
            mImageLoader = Glide.with(this);
        return mImageLoader;
    }

    /**
     * 加载错误图片
     * 可以加载原型图片
     *
     * @param view
     * @param url
     * @param resId
     */
    protected void setImageFromNet(ImageView view, String url, int resId) {
        ImageLoader.loadImage(getmImageLoader(), view, url, resId);
    }

    /**
     * 加载网络图片 不带错误图片
     *
     * @param view
     * @param url
     */
    protected void setImageFromNet(ImageView view, String url) {
        Glide.with(this).load(url).into(view);
    }

    public static void closeAllActiivty() {
        for (Activity acitivyt : activities) {
            acitivyt.finish();
        }
    }

}
